package com.niiwoo.civet.trade.service.local.kaola;

import com.niiwoo.civet.trade.dao.entity.KaoLaRealRepayment;
import com.niiwoo.civet.trade.dao.entity.KaolaRepayOrder;
import com.niiwoo.civet.trade.dao.entity.KaolaWithholdOrder;
import com.niiwoo.civet.trade.dao.mapper.KaoLaRealRepaymentMapperExt;
import com.niiwoo.civet.trade.dao.mapper.KaolaRepayOrderMapper;
import com.niiwoo.civet.trade.dao.mapper.KaolaWithholdOrderMapperExt;
import com.niiwoo.civet.trade.dto.kaola.request.GetRepayResultRequest;
import com.niiwoo.civet.trade.dto.kaola.response.GetRepayResultResponse;
import com.niiwoo.civet.trade.enums.kaola.KaolaRepayOrderRepayStatusEnum;
import com.niiwoo.civet.trade.enums.kaola.QueryTradeRecordResultStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 查询考拉理财还款结果.
 *
 * @author daizhiyue
 * @Date 2018-01-12
 */
@Service
@Slf4j
public class KaoLaLiCaiGetRepayResultService {
    @Autowired
    private KaoLaRealRepaymentMapperExt kaoLaRealrePaymentMapperExt;

    @Autowired
    private KaoLaLiCaiRequestService kaoLaLiCaiRequestService;

    @Autowired
    private KaolaWithholdOrderMapperExt kaolaWithholdOrderMapperExt;

    @Autowired
    private KaolaRepayOrderMapper kaolaRepayOrderMapper;

    public GetRepayResultResponse getRepayResult(String repaySerialNo, String subjectId) {
        GetRepayResultRequest request = new GetRepayResultRequest();
        request.setRepay_serial_no(repaySerialNo);
        request.setSubjectId(subjectId);
        GetRepayResultResponse response = kaoLaLiCaiRequestService.getRepayResult(request);
        return response;
    }

    /**
     * @return List<KaoLaRealrePayment>
     * @Description 获取当前需要获取还款结果的请求
     */
    public List<KaoLaRealRepayment> selectNeedGetRepayResultList() {
        return kaoLaRealrePaymentMapperExt.selectNeedGetRepayResultList();
    }



    /**
     * @return List<KaoLaRealrePayment>
     * @Description 获取当前需要获取还款结果的请求
     */
    public List<KaolaWithholdOrder> selectNeedDealKaolaWithholdOrderList() {
        return kaolaWithholdOrderMapperExt.selectNeedDealKaolaWithholdOrderList();
    }

    /**
     * @return List<KaoLaRealrePayment>
     * @Description 获取当前需要获取还款结果的请求
     */
    public List<KaolaWithholdOrder> selectNeedSetFail() {
        return kaolaWithholdOrderMapperExt.selectNeedSetFail();
    }

    /**
     * @return List<KaoLaRealrePayment>
     * @Description 获取当前需要获取还款结果的请求
     */
    public List<KaolaWithholdOrder> selectStatusNullKaolaWithholdOrderList() {
        return kaolaWithholdOrderMapperExt.selectStatusNullKaolaWithholdOrderList();
    }

    /**
     * @return List<KaoLaRealrePayment>
     * @Description 获取交易成功还款失败的代扣数据
     */
    public List<KaolaWithholdOrder> selectTradeWithholdOrderList() {
        return kaolaWithholdOrderMapperExt.selectTradeWithholdOrderList();
    }
    /**
     * @return List<KaoLaRealrePayment>
     * @Description 修改retry_times
     */
    public int updateByPrimaryKeySelective(KaolaRepayOrder kaolaRepayOrder) {
        return kaolaRepayOrderMapper.updateByPrimaryKeySelective(kaolaRepayOrder);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateExceptionFail(KaolaWithholdOrder kaolaWithholdOrder) {

        // 更新代扣为失败
        KaolaWithholdOrder withholdUpdater = new KaolaWithholdOrder();
        withholdUpdater.setStatus(Byte.valueOf(QueryTradeRecordResultStatusEnum.FAIL.getCode()));
        withholdUpdater.setId(kaolaWithholdOrder.getId());
        kaolaWithholdOrderMapperExt.updateByPrimaryKeySelective(withholdUpdater);

        // 更新还款为失败
        KaolaRepayOrder repayOrderUpdater = new KaolaRepayOrder();
        repayOrderUpdater.setId(kaolaWithholdOrder.getRepayOrderId());
        repayOrderUpdater.setRepayStatus(KaolaRepayOrderRepayStatusEnum.REPAY_FAIL.getValue());
        kaolaRepayOrderMapper.updateByPrimaryKeySelective(repayOrderUpdater);
    }
}
